package com.tql.mall.common.interceptor;

import com.tql.mall.common.constant.ComConstant;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: tyk
 * @Date: 2023/5/20 20:33
 * @Description: Feign拦截器
 */
@Component
public class FeignInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            // 传递用户ID和临时ID
            HttpServletRequest req = attributes.getRequest();
            requestTemplate.header(ComConstant.USER_ID, req.getHeader(ComConstant.USER_ID));
            requestTemplate.header(ComConstant.USER_TEMP_ID, req.getHeader(ComConstant.USER_TEMP_ID));
        }
    }
}
